#include <stdio.h>

void Swap(int* x, int* y)
{
  int tmp = *x;
  *x = *y;
  *y = tmp;
}

void Select(int* a, int n)
{
  int left = 0;
  int right = n-1;

  while(left < right)
  {
    int max = left;
    int min = left;
    int i = 0;
    for(i = left+1; i <= right; ++i)
    {
        if(a[i] > a[max])
          max = i;
        if(a[i] < a[min])
          min = i;
    }

    Swap(&a[left], &a[min]);
    if(max == left)
      max = min;
    Swap(&a[right], &a[max]);

    left++;
    right--;
  }
}

void Test()
{
  int arr[] = {3,2,1,-1,4,5,6,10,9,8,7};
  int sz = sizeof(arr) / sizeof(arr[0]);

  Select(arr, sz);

  int i;
  for(i = 0; i < sz; ++i)
  {
    printf("%d ", arr[i]);
  }
  printf("\n");
}

int main()
{
  
  Test();
  return 0;
}
